$(function () {
    $("#jqGrid").jqGrid({
        url: '../openingadvertisement/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true},
			{ label: '添加人', name: 'addUser', index: 'add_user', width: 80 }, 			
			{ label: '添加时间', name: 'addTime', index: 'add_time', width: 80 }, 			
			{ label: '开屏广告图片', name: 'openImage', index: 'open_image', width: 80 ,formatter:function(value){
				var img = "http://gcsbucket.oss-cn-hongkong.aliyuncs.com/"+value;
				return  '<a href='+img+'  target="_blank"><img style="width:26px;height:26px;" src='+img+' /></a>'
			}},
			{ label: '国家代码', name: 'countryCode', index: 'country_code', width: 80 }, 			
			{ label: '国家代码', name: 'countryName', index: 'country_name', width: 80 }, 			
			{ label: '语言编码', name: 'languageCode', index: 'language_code', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		openingAdvertisement: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		preview:function(){
			var file = document.getElementById("pro_file");
			console.log("open"+file.files[0])
			if (file.files && file.files[0]) {
				var reader = new FileReader();
				reader.onload = function (evt) {
					console.log("set")
					$("#pro_image").attr("src",evt.target.result);
					vm.openingAdvertisement.openImage = evt.target.result;
				}
				console.log("read")
				reader.readAsDataURL(file.files[0]);
			}
			console.log("end")
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.openingAdvertisement = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.openingAdvertisement.id == null ? "../openingadvertisement/save" : "../openingadvertisement/update";
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.openingAdvertisement),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../openingadvertisement/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../openingadvertisement/info/"+id, function(r){
                vm.openingAdvertisement = r.openingAdvertisement;
				$("#pro_image").attr("src","http://gcsbucket.oss-cn-hongkong.aliyuncs.com/"+vm.openingAdvertisement.openImage);
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});